//
//  BTHaveLearntVC.m
//  BaxTer
//
//  Created by 陈武琦 on 2016/10/31.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTHaveLearntVC.h"
#import "BTCollectionBottomMenuView.h"
#import "CWQ_tableView.h"
#import "CWQ_courseIntroductionCell.h"
#import "BTCourseDetailVC.h"
#import "Masonry.h"
@interface BTHaveLearntVC ()

@property(nonatomic, strong)CWQ_tableView *tableView;
//上拉加载的页面
@property (nonatomic, assign) NSInteger page;
@property(nonatomic, strong)NSMutableArray *dataSource;

@end

@implementation BTHaveLearntVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"已完成";
    [self setupSubviews];
}


- (void)setupSubviews {
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[CWQ_tableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain cellClass:@"CWQ_courseIntroductionCell" vc:self upload:YES];
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    _dataSource = [NSMutableArray array];
    _page = 1;
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [_tableView cwq_requestDataWithDown:YES];
    
    
//    UIButton *rightItemBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
//    [rightItemBtn setImage:[UIImage imageNamed:@"搜索_icon"] forState:UIControlStateNormal];
//    [rightItemBtn addTarget:self action:@selector(searchClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightItemBtn]];
    
}

- (void)searchClick:(UIButton *)btn {
   
}


- (void)cwq_tableView:(CWQ_tableView *)tableView requestDataWithDown:(BOOL)down {
    
    if (down) {
        self.page = 1;
    }else {
        self.page ++;
    }
    cwqHud;
    tableView.loading = YES;
    int count = 10;
    NSDictionary *param = @{@"page":@(self.page), @"rows":@(count)};
    defineWeakSelf;
    [[BTNetworkManager sharedMg] POST:BTAlreadyLearnUrl parameters:param success:^(id responseObject) {
            tableView.loading = NO;
        if (responseObject_success) {
            NSArray *array = responseObject[@"data"][@"data"];
            NSMutableArray *arr = [BTCourseModel mj_objectArrayWithKeyValuesArray:array];
            //判断是上拉刷新？  还是下拉加载？
            if (!down) {
                NSMutableArray *muArr = [weakSelf.dataSource mutableCopy];
                
                [muArr addObjectsFromArray:arr];
                weakSelf.dataSource = muArr;
                if (weakSelf.page == [responseObject[@"data"][@"totalPage"] integerValue]) {
                    tableView.footer.hidden = YES;
                }
                [tableView.footer endRefreshing];
            }else {
                if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]] || weakSelf.page == [responseObject[@"data"][@"totalPage"] integerValue]) {
                     tableView.footer.hidden = YES;
                }else{
                tableView.footer.hidden = NO;
                }
            
                self.dataSource = arr;
                [tableView.header endRefreshing];
            }

        }else {
            if (!down) {
                [tableView.footer endRefreshing];
            }else {
                
                [tableView.header endRefreshing];
            }
        }
    } failure:^(NSString *err) {
        tableView.loading = NO;
        if (weakSelf.page > 1) {
            //上拉
            [tableView.footer endRefreshing];
            weakSelf.page -= 1;
        }else {
            //下拉
            [tableView.header endRefreshing];
            if (self.dataSource.count<=0) {
                tableView.footer.hidden = YES;
            }else {
                
                tableView.footer.hidden = NO;
            }
        }
        cwqHudRequestError(err);
        
        
    }];
}

- (void)setDataSource:(NSMutableArray *)dataSource {
    _dataSource = dataSource;
    [self.tableView setupSourceArray:dataSource];
}
#pragma mark ----------------tableview代理--------------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CWQ_courseIntroductionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CWQ_courseIntroductionCellID" forIndexPath:indexPath];
    //设置cell 的分割线
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
     cell.needShowGaizhang = YES;
    [cell setCourseModel:self.dataSource[indexPath.row]];
    
    return cell;
    
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BTCourseModel *model = self.dataSource[indexPath.row];
    BTCourseDetailVC *courseDetailVC = [BTCourseDetailVC new];
    courseDetailVC.courseId = [model.id integerValue];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.navigationController pushViewController:courseDetailVC animated:YES];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
     [self.navigationController setNavigationBarHidden:NO animated:YES];
}

@end
